package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.utils.thread.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;


@Slf4j
@Service
@Transactional
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {
    @Autowired
    private FileStorageService fileStorageService;

    @Autowired
    private WmMaterialMapper wmMaterialMapper;


    /**
     * 上传素材图片
     * @param multipartFile
     * @return
     */
    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) {
        //1，如果图片格式不对，返回参数无效
        if(multipartFile == null || multipartFile.getSize() == 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2，上传图片到minio，为图片设置UUID文件名
        //2.1 UUID为文件设置文件名
        String fileName = UUID.randomUUID().toString().replace("-","");
        //2.3 获取文件后缀
        String originalFilename = multipartFile.getOriginalFilename();
        String postfix = originalFilename.substring(originalFilename.lastIndexOf("."));
        String path = null;
        //2.3 上传文件到minio
        try {
            path = fileStorageService.uploadImgFile("", fileName+postfix, multipartFile.getInputStream());
            log.info("素材上传成功,");
        } catch (IOException e) {
            e.printStackTrace();
            log.error("WmMaterialServiceImpl-上传文件失败");
        }

        //3，保存数据到数据库
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUserId(WmThreadLocalUtil.getUser().getId());
        wmMaterial.setCreatedTime(new Date());
        wmMaterial.setIsCollection((short)0);
        wmMaterial.setType((short)0);
        wmMaterial.setUrl(path);
        save(wmMaterial);

        //4，返回数据
        return ResponseResult.okResult(wmMaterial);
    }

    /**
     * 查看素材列表
     * @param dto
     * @return
     */
    @Override
    public ResponseResult materialList(WmMaterialDto dto) {
        //检验参数
        dto.checkParam();
        //分页查询
        IPage<WmMaterial> page = new Page(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<WmMaterial> wrapper = new LambdaQueryWrapper<>();
        //是否收藏
        wrapper.eq(WmMaterial::getIsCollection,dto.getIsCollection());
        //按照自媒体用户查询
        wrapper.eq(WmMaterial::getUserId,WmThreadLocalUtil.getUser().getId());
        //按照时间排序
        wrapper.orderByDesc(WmMaterial::getCreatedTime);
        page = page(page,wrapper);
        //封装page，size，total
        ResponseResult result = new PageResponseResult(dto.getPage(),dto.getSize(),(int)page.getTotal() );
        //封装数据
        result.setData(page.getRecords());
        //结果返回
        return result;
    }
}
